#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
"""
    use virt-win-reg to add guest file to vm
    @author jay.han
"""
import os
from cvirt.cnode.vm import vm_utils as vm_utils
import tempfile
host_name_reg_txt = '''
[HKLM\SYSTEM\ControlSet001\Control\ComputerName\ComputerName]
"ComputerName"="%(instance_name)s"

[HKLM\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName]
"ComputerName"="%(instance_name)s"

[HKLM\SYSTEM\ControlSet001\Services\Eventlog]
"ComputerName"="%(instance_name)s"

[HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters]
"NV Hostname"="%(instance_name)s"

[HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters]
"Hostname"="%(instance_name)s"
'''
def change_host_name_offline(instance_name, disk_path):

    _,target_xml_path = tempfile.mkstemp()

    target_xml = host_name_reg_txt % (dict(instance_name=instance_name))
    with open(target_xml_path, 'w') as f:
        f.write(target_xml)

    return vm_utils.execute('/usr/bin/virt-win-reg', '--merge', disk_path, target_xml_path,
                            check_exit_code=False,run_as_root=True)
